package com.domain.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.domain.model.SysNode;
import com.domain.model.Sysuser;
import com.domain.service.SysNodeService;

/**
 * 菜单 Controller
 * @author wenkai
 * @since 2015年4月20日 
 */
@Controller
@RequestMapping("/sysnode")
public class SysNodeController extends SuperController{
	@Resource
	private SysNodeService sysNodeService;
	@RequestMapping("/nav.html")
	@ResponseBody
	public Map<String, Object> getNavNode(HttpServletRequest request,String porCode,HttpServletResponse response){
		Map<String, Object> map=new HashMap<>();
		SysNode node=sysNodeService.getNodeByCode(porCode);
		if (node !=null  && !node.getParentsNodeId().equals("0")) {
			map.put("cNode",node);
			SysNode parentNode=sysNodeService.getNodeByCode(node.getParentsNodeId());
			if (parentNode !=null) {
				map.put("pNode", parentNode);
			}
		}
	   return map;
	}
	
	@RequestMapping("/list.html")
    public String getList(ModelMap model,HttpServletRequest request,String parentsNodeId,String porCode){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<Map<String, Object>> list=sysNodeService.getListPage(paramMap);
	    model.put(POR_CODE, porCode);
		model.put("list", list);
    	return "system/nodeList";
    }
	@RequestMapping("/edit.html")
	public String edit(HttpServletRequest request,SysNode sysNode,ModelMap model,String porCode){
		if (null !=sysNode && null !=sysNode.getId()) {
			sysNode=sysNodeService.get(sysNode.getId());
		}else {
			sysNode.setParentsNodeId(null==sysNode.getParentsNodeId() || "".equals(sysNode.getParentsNodeId())?"0":sysNode.getParentsNodeId());
		}
		model.addAttribute("entity", sysNode);
		model.addAttribute(POR_CODE, porCode);
		return "system/nodeEdit";
	}
	@RequestMapping("/save.html")
	public String save(HttpServletRequest request,SysNode sysNode,String porCode,ModelMap model){
		sysNode.setUser_id(super.getCurrentUser(request).getId());
		if (null !=sysNode && null !=sysNode.getId()) {
			sysNodeService.update(sysNode);
		}else {
			sysNode.setCreate_date(new Date());
			sysNodeService.add(sysNode);
		}
		model.addAttribute(POR_CODE, porCode);
		return "redirect:/sysnode/list.html";
	}
	@RequestMapping("/delete.html")
	@ResponseBody
	public String delete(HttpServletRequest request,SysNode sysNode,ModelMap model){
		int count=sysNodeService.isUse(sysNode.getNodeCode());
		if (count>0) {
			return "N";
		}else {
			sysNodeService.deleteById(sysNode.getId());
			return "Y";
		}
	}
	
}
